#include "pelicanosws.h"
#include <QDebug>
#include <logger.h>
#include "cfgfile.h"
#include <string.h>

PelicanoSWS::PelicanoSWS()
{
    CFGFile cfg;
    QString protocol("http://");
    QString endpoint = protocol+cfg.getServerIP()+":"+QString::number(cfg.getServerPort())+cfg.getServerPath()+"index.php?r=wsAutoRipper/wsdl&ws=1";
    _endpoint = new char[endpoint.size()+1];
    strcpy(_endpoint,endpoint.toStdString().c_str());
}
PelicanoSWS::~PelicanoSWS()
{
    if(_endpoint!=NULL)
    {
        delete _endpoint;
        _endpoint = NULL;
    }
}

bool PelicanoSWS::setState(QString id, QString idState, QString description)
{
    qDebug() << "setState(QString id, QString idState, QString description)"<<id<<idState<<description;
    Logger::log()<< "setState(QString id, QString idState, QString description)"<<id<<idState<<description;
    WsAutoRipperControllerBinding wsa;
    wsa.endpoint = _endpoint;
    qDebug() << "wsa.endpoint: "<<wsa.endpoint;
    bool response;
    std::string r_id(id.toStdString());
    std::string r_idState(idState.toStdString());
    std::string r_description(description.toStdString());

    int result = wsa.ns7__setState(r_id,r_idState,r_description,response);

    if(result==SOAP_OK)
    {
        qDebug() << "setState() SOAP OK";
        Logger::log()<< "setState() SOAP OK";
    }
    else
    {
        qDebug() << "setState() SOAP Error:"<<result;
        Logger::log()<< "setState() SOAP Error:"<<result;
    }
    return response;
}

QString PelicanoSWS::getProcessName()
{
    qDebug() <<"getProcessName";
    Logger::log()<<"getProcessName";

    WsAutoRipperControllerBinding wsa;
    wsa.endpoint = _endpoint;
    qDebug() << "wsa.endpoint: "<<wsa.endpoint;
    std::string response;

    int result = wsa.ns7__getProcessName(response);

    if(result==SOAP_OK)
    {
        qDebug() << "getProcessName() SOAP OK";
    }
    else
    {
        qDebug() << "getProcessName() SOAP Error";
    }
    return response.c_str();
}
QString PelicanoSWS::getNewName(QString idProcess, QString label,QString size)
{
    qDebug() <<"getNewName: "<<idProcess<<" "<<label;
    Logger::log()<<"getNewName: "<<idProcess<<" "<<label;

    WsAutoRipperControllerBinding wsa;
    wsa.endpoint = _endpoint;
    qDebug() << "wsa.endpoint: "<<wsa.endpoint;
    std::string response;
    std::string r_id(idProcess.toStdString());
    std::string r_label(label.toStdString());
    std::string r_size(size.toStdString());

    //int ns7__getNewName(std::string idProcess, std::string label, std::string &return_) { return soap ? soap_call_ns7__getNewName(soap, endpoint, NULL, idProcess, label, return_) : SOAP_EOM; };
    int result = wsa.ns7__getNewName(r_id, r_label, r_size, response);
    if(result==SOAP_OK)
    {
        qDebug() << "getNewName() SOAP OK: "<<response.c_str();
    }
    else
    {
        qDebug() << "getNewName() SOAP Error: "<<result;
    }
    return response.c_str();
}


QString PelicanoSWS::ripperStart(QString idDisc,QString processName,QString label)
{
    qDebug() << "QString PelicanoSWS::ripperStart(QString idDisc,QString processName,QString label)";
    qDebug() << "idDisc:"<<idDisc;
    qDebug() << "processName"<<processName;
    qDebug() << "label"<<label;

    WsAutoRipperControllerBinding wsa;
    wsa.endpoint = _endpoint;
    qDebug() << "wsa.endpoint: "<<wsa.endpoint;
    std::string response;
    std::string idProcess(processName.toStdString());
    std::string r_idDisc(idDisc.toStdString());
    std::string r_label(label.toStdString());
    int result = wsa.ns7__ripperStart(r_idDisc,idProcess,r_label,response);

    if(result==SOAP_OK)
    {
        qDebug() << "ripperStart() SOAP OK";
    }
    else
    {
        qDebug() << "ripperStart() SOAP Error:"<<result;
    }
    qDebug() << "response.c_str():"<<response.c_str();;
    return response.c_str();
}

bool PelicanoSWS::setPercentage(QString id, QString  percentage)
{
    qDebug() << "setPercentage(QString id, std::string percentage)"<<id<<percentage;
    WsAutoRipperControllerBinding wsa;
    wsa.endpoint = _endpoint;
    qDebug() << "wsa.endpoint: "<<wsa.endpoint;
    bool response;
    std::string r_id(id.toStdString());
    std::string r_percentage(percentage.toStdString());

    int result = wsa.ns7__setPercentage(r_id,r_percentage,response);

    if(result==SOAP_OK)
    {
        qDebug() << "setPercentage() SOAP OK";
    }
    else
    {
        qDebug() << "setPercentage() SOAP Error";
    }
    return response;
}

StepResponse PelicanoSWS::getNextStep(QString id)
{
    qDebug() << "getNextStep(QString id)"<<id;
    WsAutoRipperControllerBinding wsa;
    wsa.endpoint = _endpoint;
    qDebug() << "wsa.endpoint: "<<wsa.endpoint;
    ns7__getNextStepResponse responseWs;
    std::string r_id(id.toStdString());
    StepResponse response;
    int result = wsa.ns7__getNextStep(r_id, responseWs);

    if(result==SOAP_OK)
    {
        qDebug() << "getNextStep() SOAP OK";
        response.step = QString::fromStdString(responseWs.return_->next_USCOREstep);
        response.id_auto_ripper = QString::fromStdString(responseWs.return_->id_USCOREauto_USCOREripper);
        qDebug() << "response.step: "<<response.step;
        qDebug() << "response.file_name : "<<response.file_name;
        qDebug() << "response.id_auto_ripper : "<<response.id_auto_ripper;
    }
    else
    {
        qDebug() << "getNextStep() SOAP Error";
    }
    return response;
}

